<!DOCTYPE html>
<html class="x-admin-sm">

<head>
    <meta charset="UTF-8">
    <title>鲜知后台管理系统-导入学校</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="stylesheet" href="/static/css/font.css">
    <link rel="stylesheet" href="/static/css/xadmin.css">
    <link rel="stylesheet" href="/static/css/webuploader.css">
    <script type="text/javascript" src="/static/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="/static/js/xadmin.js"></script>
    <script type="text/javascript" src="/static/js/jquery.cookie.js"></script>
    <script type="text/javascript" src="/static/js/base64.decode.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
    <style>
        .webuploader-pick {
            background: #009688 !important;
            height: 10px !important;
            line-height: 10px !important;
        }
    </style>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-row">
        <form class="layui-form" id="data-form">
            {{.xsrfdata}}
            <div class="layui-form-item">
                <label for="username" class="layui-form-label">
                    <span class="x-red">*</span>所属省
                </label>
                <div class="layui-input-inline">
                    <select id="province" name="province_id" lay-filter="province" lay-verify="">
                        <option value="0">请选择省</option>
                        {{range .provinceList}}
                            <option value="{{.Id}}">{{.Name}}</option>
                        {{end}}
                    </select>
                </div>
                <div class="layui-form-mid layui-word-aux">

                </div>
            </div>
            <div class="layui-form-item">
                <label for="username" class="layui-form-label">
                    <span class="x-red">*</span>所属市
                </label>
                <div class="layui-input-inline">
                    <select id="city" name="city_id" lay-verify="">
                        <option value="0">请选择市</option>
                    </select>
                </div>
                <div class="layui-form-mid layui-word-aux">

                </div>
            </div>
            <div class="layui-form-item" style="margin-bottom: 0;">
                <label class="layui-form-label">
                    <span class="x-red">*</span>excel文件
                </label>
                <div class="layui-input-inline">
                    <div id="picker">选择文件</div>
                </div>
                <div class="layui-form-mid layui-word-aux">
                    <a target="_blank" href="{{urlfor "admin.DownloadController.Index" ":name" "schoolExample.xlsx"}}">示例excel下载</a>
                </div>
            </div>
            <div style="width: 200px;padding-left: 105px;margin-bottom: 15px;">
                <div id="progress-bar" class="layui-progress layui-progress-big layui-hide" lay-filter="excelFile" lay-showPercent="yes">
                    <div class="layui-progress-bar layui-bg-green" lay-percent="0%"></div>
                </div>
            </div>
            <input type="hidden" name="excel_file_path" id="excel_file_path">
            <div class="layui-form-item">
                <label class="layui-form-label"></label>
                <button class="layui-btn" lay-filter="do-submit" lay-submit="">导入</button>
            </div>
        </form>
    </div>
</div>
<script type="text/javascript" src="/static/js/jquery.min.js"></script>
<script type="text/javascript" src="/static/js/webuploader.min.js"></script>
<script>
    layui.use(['form', 'layer', 'element'],
        function () {
            $ = layui.jquery;
            var form = layui.form,
                layer = layui.layer,
                canSubmit = true,
                element = layui.element,
                xsrf = base64_decode(Cookies.get("_xsrf").split("|")[0]);

            var uploader = WebUploader.create({
                auto: true,
                swf: '/static/js/Uploader.swf',
                server: '{{urlfor "admin.UploadController.SingleFile"}}',
                pick: {id: "#picker", multiple: false},
                resize: false,
                fileVal: "excel_file"
            });

            uploader.on( 'fileQueued', function(file) {
                $("#progress-bar").removeClass("layui-hide");
            });

            uploader.on( 'uploadProgress', function(file, percentage) {
                element.progress('excelFile', percentage * 100 + '%');
            });

            uploader.on( 'uploadSuccess', function(file, res) {
                if (res.code == 1) {
                    $("#excel_file_path").val(res.result);
                    setTimeout(function () {
                        $("#progress-bar").addClass("layui-hide");
                    }, 2000);
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            });

            form.on("select(province)", function (data) {
                var id = data.value;
                $.ajax({
                    url: "/common/get_region_by_parent",
                    type: "POST",
                    data: {_xsrf: xsrf, id: id, level: 2},
                    success: function (res) {
                        $("#city").html(res);
                        form.render("select");
                    }
                });
            });

            //监听提交
            form.on('submit(do-submit)',
                function (data) {

                    if ($("#province").val() <= 0) {
                        layer.msg("请选择省", {icon: 2});
                        return false;
                    }
                    if ($("#city").val() <= 0) {
                        layer.msg("请选择市", {icon: 2});
                        return false;
                    }
                    if ($("#excel_file_path").val().length <= 0) {
                        layer.msg("请上传excel文件", {icon: 2});
                        return false;
                    }

                    if (!canSubmit) return false;
                    var loadIndex = layer.load();
                    canSubmit = false;

                    $.ajax({
                        url: "{{urlfor "admin.SchoolController.DoExcelAdd"}}",
                        type: "POST",
                        data: $("#data-form").serialize(),
                        dataType: "json",
                        success: function (res) {

                            layer.close(loadIndex);
                            canSubmit = true;

                            if (res.status != 1) {
                                layer.msg(res.msg, {icon: 2});
                                return false;
                            }

                            layer.alert(res.msg, {icon: 6}, function () {
                                //关闭当前frame
                                xadmin.close();
                                // 可以对父窗口进行刷新
                                xadmin.father_reload();
                            });

                        }
                    });

                    return false;

                });

        });
</script>
</body>

</html>
